<?php
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/11 17:15
 * Desc: 富文本数据处理
 */
trait Trait_Api_Content
{

    /**
     * 富文本编辑器添加域名
     * @param $content
     * @return array|string|string[]|null
     */
    public function content_add_domain($content)
    {
        //删除视频链接的某些属性
        $content = St_Functions::del_video_attr($content);

        //给视频重置宽高
        $content = St_Functions::set_video_w_h($content);

        $pattern = "/<[img|IMG|video|VIDEO].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg|\.mp4]))[\'|\"].*?[\/]?>/i";

        return preg_replace_callback(
            $pattern,
            function ($ma) {
                $newUrl = $ma[1];
                if (strpos($ma[1], 'http') === false) {
                    $newUrl = $this->img_url($newUrl);
                }
                return str_replace($ma[1], $newUrl, $ma[0]);
            }, $content);
    }
}
